<template>
	<view class="bg">
		<view class="biaodan">
			<view>
				<input type="text" placeholder="请输入您的电话号码" v-model="phone" autocomplete="off" />
			</view>

			<view>
				<input type="password" placeholder="最少5位" v-model="userpwd" autocomplete="off" />
			</view>
			<view>
				<button @click="enrllo">登录</button>
			</view>
			<view class="text" ><text @click="toLongin">注册</text></view>
		</view>
	</view>
</template>

<script setup>
	import {enroll} from '@/api/users.js'
	import {
		ref
	} from "vue";
	import {useStore} from 'vuex'
	let store = useStore()
	let phone = ref('')
	let userpwd = ref('')
	const enrllo = () => {
		let phonetel = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
		if(phonetel.test(phone.value)&&userpwd.value.length>4){
			enroll(phone.value,userpwd.value).then(res=>{
				if(res.data.code==200){
					store.commit('updateusers',res.data.data[0])
					res.data.data[0].times = new Date().getTime()
					uni.setStorageSync('usersdata',JSON.stringify(res.data.data[0]))
					uni.switchTab({
						url: '/pages/index/index'
					});
					
				}else{
					uni.showToast({
						title: '没有该用户',
						icon: "error"
					});
				}
			})
		}else{
			uni.showToast({
				title: '格式错啦',
				icon: "error"
			});
		}
	}
	const toLongin = ()=>{
		uni.navigateTo({
			url:'/pages/logon/logon'
		})
	}
	let val =  uni.getStorageSync('usersdata')
	if(val){
		let shijiancha = (new Date().getTime()-JSON.parse(val).times)/1000
		if(shijiancha > 2530080){
			uni.removeStorageSync('usersdata')
		}else{
			store.commit('updateusers',JSON.parse(val))
			uni.switchTab({
				url: '/pages/index/index'
			});
		}
	}
</script>

<style lang="scss">
		
	.bg{
		width: 100vw;
		height: 1000rpx;
		padding-top: 450rpx;
		background-image: url('http://175.178.117.99:8888/down/fGEf9y6dGTEv.png') ;
		background-repeat: no-repeat;
		background-size: 100% 100% ;
	}
	.biaodan {
		width: 650rpx;
		margin: 0 auto;
		
		
		>view {
			width: 650rpx;
			height: 120rpx;
			

			input {
				padding-left: 30rpx;
				font-size: 40rpx;
				height: 90rpx;
				line-height: 90rpx;
				border-bottom: 1rpx solid #e8dde5;
				color: #ffffff;
			}
			button{
				background-color: #f50f50;
				color: #ffffff;
			}
			
		}
		.text{
			text-align: right;
			color: #7595ff;
		}


	}
</style>
